package TetrisFlash.Screen 
{
	import starling.display.QuadBatch;
	import starling.events.EnterFrameEvent;
	import starling.events.Event;
	import starling.display.Button;
	import starling.display.Image;
	import starling.utils.deg2rad;
	
	import TetrisFlash.Screen.MainGameScreen;
	import TetrisFlash.Game;
	import TetrisFlash.GameLoadData.AssetsTexture;
	import TetrisFlash.GameLoadData.SoundManager;
	import com.greensock.TweenLite;
	/**
	 * ...
	 * @author DANTRUONG
	 */
	public class Welcome extends Screen 
	{
		private var bg:Image;
		
		private var playBtn:Button;
		
		public function Welcome(_game: Game) 
		{
			super(_game);
		}
		
		override public function initScreen():void {
			trace("Welcome screen initialized");
			
			bg = new Image(AssetsTexture.getTetrisAtlas().getTexture("backgroundMenu"));
			this.addChild(bg);
			
			playBtn = new Button(AssetsTexture.getTetrisAtlas().getTexture("ButtonStart"));
			playBtn.x = 295;
			playBtn.y = 239;
			this.addChild(playBtn);
			//add animation for play button
			this.addEventListener(Event.ENTER_FRAME, btnAnimation);
			
			//add trigger click on MainMenu
			this.addEventListener(Event.TRIGGERED, onMainMenuClick);
			
			myGame.soundManager.playBackSoundMenu();
		}
		
		private function btnAnimation(event: EnterFrameEvent):void 
		{
			var currentDate:Date = new Date();
			playBtn.y = 239 + (Math.cos(currentDate.getTime() * 0.003) * 4);
		}
		
		private function onMainMenuClick(event:Event):void 
		{
			var buttonClicked:Button = event.target as Button;
			if((buttonClicked as Button) == playBtn)
			{
				myGame.soundManager.stopBackSoundMenu();
				myGame.soundManager.play(SoundManager.SOUND_CHOOSE);
				myGame.setScreen(MainGameScreen);
			}
		}
		
		override public function destroyScreen():void {
			if (this.contains(bg)) {
				this.removeChild(bg);
			}
			if (this.contains(playBtn)) {
				this.removeChild(playBtn);
			}
			bg.dispose();
			playBtn.dispose();
		}	
	}

}